/**
 * 
 */
package com.furschina.struts2;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.InterceptorRefs;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.stereotype.Controller;

import cn.wooshai.core.struts2.WooshaiAction;

import com.furschina.Constants;
import com.furschina.model.User;
import com.furschina.service.UserService;

/**
 * @author iday
 * 
 */
@Controller
@InterceptorRefs({ @InterceptorRef(Constants.DEFAULT_STACK_REF),
		@InterceptorRef(Constants.VALIDATOR_REF) })
@Result(name = WooshaiAction.SUCCESS, type = "redirect", location = "home.xhtml")
public class RegisterAction extends CaptchaAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1481844016007720315L;
	static Log LOG = LogFactory.getLog(RegisterAction.class);

	@Valid
	private User user;
	@NotNull
	private String repassword;
	private String code;
	private String id;

	private UserService userService;
	
	/**
	 * @param userService
	 *            the userService to set
	 */
	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @return the id
	 */
	public String getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(String id) {
		this.id = id;
	}

	/**
	 * @return the user
	 */
	public User getUser() {
		return user;
	}

	/**
	 * @param user
	 *            the user to set
	 */
	public void setUser(User user) {
		this.user = user;
	}

	/**
	 * @return the repassword
	 */
	public String getRepassword() {
		return repassword;
	}

	/**
	 * @param repassword
	 *            the repassword to set
	 */
	public void setRepassword(String repassword) {
		this.repassword = repassword;
	}

	/**
	 * @return the code
	 */
	public String getCode() {
		return code;
	}

	/**
	 * @param code
	 *            the code to set
	 */
	public void setCode(String code) {
		this.code = code;
	}

	public String index() {
		return INDEX;
	}

	public String show() {
		return SHOW;
	}

	public String create() {
		this.userService.createLocalUser(user);
		return SUCCESS;
	}

	public String val() {
		return null;
	}

	public boolean validateEmail() {
		User u = this.userService.findByUseremail(user.getEmail());
		return u == null;
	}

	public boolean validateUsername() {
		User u = this.userService.findByUsername(user.getUsername());
		return u == null;
	}

}
